<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Http\Controllers\Admin;

use Carbon\Carbon;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Traits\AdminHelp;
use App\Models\BirthdayTool;
use App\Exceptions\ResourceException;
use Overtrue\ChineseCalendar\Calendar;
use Encore\Admin\Controllers\AdminController;

class BirthdayToolController extends AdminController
{
    use AdminHelp;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '农/新历生日订阅列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BirthdayTool());

        $grid->column('id', 'ID')->sortable();
        $grid->column('name', '姓名');
        $grid->column('lunar_calendar', '农历生日');
        $grid->column('new_calendar', '新历生日');
        $grid->column('description', '描述');
        $grid->column('status', '是否订阅')->switch([0 => false, 1 => true]);

        $grid->expandFilter();

        $grid->setActionClass(Grid\Displayers\Actions::class);

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('name', '姓名');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BirthdayTool::query()->findOrFail($id));

        $show->field('id', 'ID');
        $show->field('name', '姓名');
        $show->field('lunar_calendar', '农历生日');
        $show->field('new_calendar', '新历生日');
        $show->field('description', '描述');
        $show->field('status', '是否订阅');
        $show->field('admin_user_id', '操作人');
        $show->field('created_at', '创建时间');
        $show->field('updated_at', '修改时间');

        return $show;
    }

    /**
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BirthdayTool());

        $form->text('name', '姓名');

        $form->select('type', '生日类型')
            ->options(BirthdayTool::TYPE)
            ->default('lunar')
            ->when('solar', function (Form $form) {
                $form->date('new_calendar', '新历生日');
            })
            ->when('lunar', function (Form $form) {
                $form->date('lunar_calendar', '农历生日');
            });

        $form->textarea('description', '描述');
        $form->switch('status', '是否订阅')->default(true);

        $form->ignore('type');

        $this->autoAdminUserSaving($form);

        $form->saving(function (Form $form) {
            if (empty($form->lunar_calendar) && empty($form->new_calendar)) {
                throw new ResourceException('农历和新历生日必须选一个填写！');
            }

            $type = \request('type') ?? null;

            $calendar = new Calendar();

            switch ($type) {
                case 'lunar':
                    $lunarCalendar = Carbon::createFromFormat('Y-m-d', $form->lunar_calendar);
                    $result = $calendar->lunar(
                        $lunarCalendar->year,
                        $lunarCalendar->month,
                        $lunarCalendar->day
                    );
                    $newCalendar = $result['gregorian_year'] . '-' . $result['gregorian_month'] . '-' . $result['gregorian_day'];
                    $form->new_calendar = $newCalendar;

                    break;
                case 'solar':
                    $newCalendar = Carbon::createFromFormat('Y-m-d', $form->new_calendar);
                    $result = $calendar->solar($newCalendar->year, $newCalendar->month, $newCalendar->day);
                    $lunarCalendar = $result['lunar_year'] . '-' . $result['lunar_month'] . '-' . $result['lunar_day'];
                    $form->lunar_calendar = $lunarCalendar;

                    break;
                default:
                    throw new ResourceException('生日类型为空，请选择生日类型');
            }
        });

        return $form;
    }
}
